const { getClassifyList, getClassifyDetails } = require('../../api/index.js')
const app = getApp()
Component({
  data: {
    inavHeight: 0,
    inavTop: 0,
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    categoryList: [],
    currentCategory: {}
  },
  methods: {
    getClassifyList() { //分类目录及数据
      getClassifyList().then(res => {
        console.log(res)
        this.setData({
          categoryList: res.categoryList,
          currentCategory: res.currentCategory
        })
      })
    },
    async swichNav(e) { // 切换
      if (this.data.currentTab === e.currentTarget.dataset.current) {
        return false;
      } else {
        this.setData({
          currentTab: e.currentTarget.dataset.current
        })
        const { currentCategory } = await getClassifyDetails(e.currentTarget.id)
        this.setData({
          currentCategory
        })
      }
    }
  },
  pageLifetimes: {
    show() {
      // tabbar切换
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 2
        })
      }
      // 请求数据
      this.getClassifyList()
      // 调用胶囊数据
      this.setData({
        inavHeight: app.globalData.inavHeight,
        inavTop: app.globalData.inavTop
      })
      // 获取当前设备的宽高
      var that = this;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            winWidth: res.windowWidth,
            winHeight: res.windowHeight
          });
        }
      });
    }
  }
})
